//
//  KARepayDetailSectionHeader.swift
//  kano
//
//  Created by Frank on 2024/9/19.
//

import UIKit

class KARepayDetailSectionHeader: UIView {
    
    lazy var titleLabel: UILabel = {
        let label = UILabel()
        label.numberOfLines = 1
        label.font = UIFont.systemFont(ofSize: kscale(13), weight: .bold)
        label.textColor = .color323135
        label.text = "1/3 selected"
        return label
    }()
    
    var var_selectHandler: ((_ select: Bool) -> ())?
    
    lazy var selectBtn: UIButton = {
        let button = UIButton()
        button.setTitle("select all ", for: .normal)
        button.setTitleColor(.color8A8891, for: .normal)
        button.titleLabel?.font = kFontSize(kscale(12))
        button.setImage(UIImage(named: "repay_unsel"), for: .normal)
        button.setImage(UIImage(named: "repay_sel"), for: .selected)
        button.addTarget(self, action: #selector(selectAllBtnClick(_:)), for: .touchUpInside)
        return button
    }()
    @objc func selectAllBtnClick(_ sender: UIButton) {
        if (var_selectHandler != nil) {
            sender.isSelected = !sender.isSelected
//            let imageName = sender.isSelected ? "repay_sel" : "repay_unsel"
//            sender.setImage(UIImage(named: imageName), for: .normal)
            
            var_selectHandler!(sender.isSelected)
        }
    }
    
    lazy var unfoldTitle: UILabel = {
        let label = UILabel()
        label.numberOfLines = 1
        label.font = UIFont.systemFont(ofSize: kscale(12), weight: .regular)
        label.textColor = .colorAEACB3
        label.text = "unfold"
        return label
    }()
    lazy var var_arrow: UIImageView = {
        let var_arrow = UIImageView(image: UIImage(named: "common_arrow_down"))
        return var_arrow
    }()
 
    lazy var var_lineView: UIView = {
        let var_lineView = UIView()
        var_lineView.backgroundColor = .colorE2E1E3
        return var_lineView
    }()
  
    var var_handler: (() -> ())?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = .white
        let var_tap = UITapGestureRecognizer(target: self, action: #selector(tapClick(_ :)))
        addGestureRecognizer(var_tap)

        ka_setUI()
    }
    
    @objc func tapClick(_ sender: UITapGestureRecognizer) {
        if (var_handler != nil) {
            var_handler!()
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func ka_setUI() {
        backgroundColor = .white
        
        addSubview(titleLabel)
        addSubview(selectBtn)
        addSubview(unfoldTitle)
        addSubview(var_arrow)
        addSubview(var_lineView)

        ka_addTopCorner(withRadius: kscale(16))
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        titleLabel.snp.makeConstraints { make in
            make.left.equalTo(kscale(12))
            make.centerY.equalTo(self)
        }
   
        unfoldTitle.snp.makeConstraints { make in
            make.left.equalTo(titleLabel.snp.right).offset(kscale(8))
            make.centerY.equalTo(self)
        }
        var_arrow.snp.makeConstraints { make in
            make.size.equalTo(CGSizeMake(kscale(16), kscale(16)))
            make.left.equalTo(unfoldTitle.snp.right).offset(kscale(4));
            make.centerY.equalTo(self)
        }
        selectBtn.snp.makeConstraints { make in
            make.right.equalTo(kscale(-12))
            make.centerY.equalTo(var_arrow)
        }
        var_lineView.snp.makeConstraints { make in
            make.left.equalTo(kscale(12))
            make.right.equalTo(kscale(-12))
            make.height.equalTo(1)
            make.bottom.equalTo(-8)
        }
        
        selectBtn.imageEdgeInsets = UIEdgeInsets(top: 0, left: selectBtn.titleLabel!.intrinsicContentSize.width, bottom: 0, right: -selectBtn.titleLabel!.intrinsicContentSize.width)
        selectBtn.titleEdgeInsets = UIEdgeInsets(top: 0, left: -selectBtn.imageView!.intrinsicContentSize.width, bottom: 0, right: selectBtn.imageView!.intrinsicContentSize.width)
    }
  
}

